﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Tsanie.DmPoster.Utils {
    public class StringUtil {
        public static string UrlEncode(string str) {
            return str
                .Replace(".", "%2E")
                .Replace("-", "%2D")
                .Replace(" ", "%20")
                .Replace(":", "%3A")
                .Replace("\"", "%22")
                .Replace("[", "%5B")
                .Replace(",", "%2C")
                .Replace("]", "%5D")
                ;
        }
        public static int StringWidth(string str, int size) {
            if (string.IsNullOrEmpty(str))
                return 0;

            int c1 = 0; // 全角个数
            int c2 = 0; // 半角个数
            for (int i = 0; i < str.Length; i++) {
                if (str[i] > 255)
                    c1++;
                else
                    c2++;
            }
            float fSize = size + 2;
            float hSize = size + 3;
            switch (size) {
                case 12:
                    fSize = 13.125f;
                    hSize = 14.13513513513514f;
                    break;
                case 16:
                    fSize = 17.16666666666667f;
                    hSize = 18.17241379310345f;
                    break;
                case 18:
                    fSize = 19.20833333333333f;
                    hSize = 20.2f;
                    break;
                case 25:
                    fSize = 27.27777777777778f;
                    hSize = 28.27777777777778f;
                    break;
                case 36:
                    fSize = 37.41666666666667f;
                    hSize = 38.38461538461538f;
                    break;
                case 45:
                    fSize = 47.5f;
                    hSize = 48.47619047619048f;
                    break;
            }
            return (int)(c1 * fSize + c2 * hSize / 2);
        }
        public static int GetTopOffset(int size) {
            return 0;
        }
        public static string StringFromColorInt(int color) {
            return "#" + (color & 0xffffff).ToString("X6");
        }
        public static string StringFromColor(Color color) {
            return StringFromColorInt(color.ToArgb());
        }
        public static Color ColorFromString(string color) {
            int c;
            if (color[0] == '#') {
                c = int.Parse(color.Substring(1), System.Globalization.NumberStyles.HexNumber);
            } else {
                c = int.Parse(color);
            }
            return ColorFromInt(c);
        }
        public static Color ColorFromInt(int color) {
            return Color.FromArgb(color | -16777216);
        }
        public static int IntColorFromString(string color) {
            int c;
            if (color[0] == '#') {
                c = int.Parse(color.Substring(1),
                    System.Globalization.NumberStyles.HexNumber);
            } else {
                c = int.Parse(color);
            }
            return c;
        }
        public static DateTime DateFormString(string date) {
            DateTime time;
            if (!DateTime.TryParse(date, out time)) {
                time = DateTime.Now;
            }
            return time;
        }
        public static DateTime DateFromLong(long date) {
            return new DateTime(621356256000000000L + date * 10000000L);
        }
        public static Level LevelFromString(string level) {
            if (level == "普通会员") {
                return Level.Member;
            } else if (level == "新番搬运") {
                return Level.Porter;
            } else if (level == "字幕君") {
                return Level.Commenter;
            } else if (level == "VIP") {
                return Level.Vip;
            } else if (level == "真·职人") {
                return Level.Major;
            } else if (level == "Admin") {
                return Level.Admin;
            }
            return Level.Guest;
        }
        public static string StringFromLevel(Level level) {
            if (level == Level.Member) {
                return "普通会员";
            } else if (level == Level.Porter) {
                return "新番搬运";
            } else if (level == Level.Commenter) {
                return "字幕君";
            } else if (level == Level.Vip) {
                return "VIP";
            } else if (level == Level.Major) {
                return "真·职人";
            } else if (level == Level.Admin) {
                return "Admin";
            }
            return "游客";
        }
    }
}
